package com.zhongwang.mms.module.material.service;

import com.zhongwang.mms.gen.entity.MInventory;
import com.zhongwang.mms.module.material.dao.MInventoryDao;
import com.zhongwang.mms.module.system.log.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.zhongwang.mms.util.BeanUtil.copy;


/**
 * @Description: 库存信息编辑 服务层
 * @Author: China.wsg
 * @Date: 2020/06/10 10:16
 */
@Service
public class MInventoryEditService {

    @Autowired
    private MInventoryDao mInventoryDao;
    @Autowired
    private LogService logService;
    @Autowired
    private MInventoryService inventoryService;

    /**
     * 库存信息编辑
     * 暂定更改信息：项目名称、特殊说明、回货时间
     *
     * @param inventory 保存信息
     * @return json 数据
     */
    @Transactional
    public String edit(MInventory inventory) {

        MInventory oldInventory = mInventoryDao.selectByPrimaryKey(inventory.getItyId());

        MInventory newInventory = new MInventory();
        copy(oldInventory, newInventory);
        newInventory.setItyProName(inventory.getItyProName());
        newInventory.setItySpec(inventory.getItySpec());
        newInventory.setItyBackDate(inventory.getItyBackDate());

        String checkMsg = editCheck(oldInventory, newInventory); // 校验结果

        if (checkMsg.equals("无更改") || checkMsg.equals("被标记")) {
            return checkMsg;
        } else if (checkMsg.equals("合并")) {

            // 减少本身
            inventoryService.subInventory(oldInventory, newInventory.getItyNum(), newInventory.getItyWeight(), newInventory.getItyAvaNum(), newInventory.getItyAvaWeight());

            // 原料量添加到新记录
            newInventory = inventoryService.addInventory(newInventory, 3);

        } else if (checkMsg.equals("不合并")) {

            // 更新原料在库
            mInventoryDao.updateByPrimaryKey(newInventory);
        }

        if (!checkMsg.equals("被标记")) {
            logService.saveLog("库存信息编辑", "项目名称：" + oldInventory.getItyProName() + "->" + inventory.getItyProName() +
                    "<br>特殊说明：" + oldInventory.getItySpec() + "->" + inventory.getItySpec() +
                    "<br>回货时间：" + oldInventory.getItyBackDate() + "->" + inventory.getItyBackDate() +
                    "<br>库存主键：" + oldInventory.getItyId() + "->" + newInventory.getItyId());
        }

        return "";
    }

    /**
     * 判断提交的记录是否会被合并
     * 暂定更改信息：项目名称、特殊说明、回货时间
     *
     * @param oldInventory 保存信息
     * @param newInventory 现库存在库信息
     * @return json 数据
     */
    public String editCheck(MInventory oldInventory, MInventory newInventory) {

        // 判断 字段无任何变化时 直接返回成功
        if (oldInventory.getItyProName().equals(newInventory.getItyProName())
                && oldInventory.getItySpec().equals(newInventory.getItySpec())
                && oldInventory.getItyBackDate().equals(newInventory.getItyBackDate())) {
            return "无更改";
        }

        // 标记被占用
        if (oldInventory.getItyNum().compareTo(oldInventory.getItyAvaNum()) != 0 || oldInventory.getItyWeight().compareTo(oldInventory.getItyAvaWeight()) != 0) {
            return "被标记";
        }

        // 判断 更改后数据是否将被合并，如果被合并将不允许本次修改
        if (inventoryService.findMInventoryByQuery(newInventory) != null) {
            return "合并";
        }

        return "不合并";
    }


}
